package com.dayuanit.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import redis.clients.jedis.Jedis;

public class JedisTest {
	
	private static final String HOST = "192.168.2.161";
	private static final int PORT = 6379;
	
	public static void testString(Jedis jedis) throws Exception {
		ObjectMapper om = new ObjectMapper();
		
		
//		Dog dog = new Dog();
//		dog.setAge(10);
//		dog.setName("waicai");
//		jedis.set("dayuan:dog", om.writeValueAsString(dog));
//		
//		String msg = jedis.get("dayuan:dog");
//		System.out.println(msg);
//		
//		Dog dog2 = om.readValue(msg, Dog.class);
//		System.out.println(dog2.getAge());
//		System.out.println(dog2.getName());
		
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "jim");
		map.put("age", "10");
		list.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "tom");
		map.put("age", "12");
		list.add(map);
		
		String ss = om.writeValueAsString(list);
		System.out.println(ss);
		
		List<Map<String, String>> dogList = om.readValue(ss, List.class);
		for (Map<String, String> d : dogList) {
			System.out.println(d.get("name"));
			System.out.println(d.get("age"));
		}
		
		//jedis.set("dayuanit:listmap:dog", ss);
		
	}
	
	public static void testHash(Jedis jedis) {
		jedis.hset("dayuanit:hset:dog", "name", "wangcai");
		jedis.hset("dayuanit:hset:dog", "age", "10");
		
		String msg = jedis.hget("dayuanit:hset:dog", "name");
		System.out.println(msg);
		
		msg = jedis.hget("dayuanit:hset:dog", "age");
		System.out.println(msg);
		
		Map<String, String> map = jedis.hgetAll("dayuanit:hset:dog");
		msg = map.get("name");
		System.out.println("all name:" + msg);
		
		msg = map.get("age");
		System.out.println("all age:" + msg);
		
	}
	
	
	public static void testSet(Jedis jedis) {
		long rows = jedis.sadd("dayuanit:set:name", "jim","tom","sam");
		System.out.println(rows);
		rows = jedis.sadd("dayuanit:set:name", "jack");
		System.out.println(rows);
		
		Set<String> set = jedis.smembers("dayuanit:set:name");
		for (String msg : set) {
			System.out.println(msg);
		}
		
		
	}
	
	public static void main(String[] args) throws Exception {
		Jedis jedis = new Jedis(HOST, PORT);
		
		//测试string类型
		JedisTest.testString(jedis);
		
////		JedisTest.testHash(jedis);
//		JedisTest.testSet(jedis);
		
	}
}

class Dog{
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}
